﻿using System;
using System.Linq;
using System.Reflection;
using FluentNHibernate.Cfg;
using log4net.Config;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Validator.Cfg.Loquacious;
using NHibernate.Validator.Engine;
using NHibernateDown2Earth.Core.Entities;
using FluentConfiguration = NHibernate.Validator.Cfg.Loquacious.FluentConfiguration;
using NHibernateDown2Earth.Core;

namespace NHibernateDown2Earth
{
	class Program
	{
		static void Main(string[] args)
		{
			AppConfigure();

			// write your test theories here ...
			using(var session = ApplicationContext.SessionFactory.OpenSession())
			{
				var cat1 = new Category() { Name = "Beverage" };
				session.Save(cat1);

				var product1 = new Product();
				product1.Cost = 100.0;
				product1.Name = "Milk";
				product1.QtyInStock = 10;
				product1.ProductType = cat1;

				new RepositoryBase<Product>(session).Save(product1);
			}

			Console.Read();
		}

		private static void AppConfigure()
		{
			#region Configuration Log4Net

			// To see the SQLs run the application inside VisualStudio
			// watching the Output window
			XmlConfigurator.Configure();

			#endregion

			#region NHibernate configuration

			var conf = Fluently.Configure(new Configuration().Configure())
				.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Product>())
				.BuildConfiguration();

			#endregion

		}

		private static void BuildSchema(Configuration config)
		{
			// delete the existing db on each run
			if (File.Exists(DbFile))
				File.Delete(DbFile);

			// this NHibernate tool takes a configuration (with mapping info in)
			// and exports a database schema from it
			new SchemaExport(config)
				.Create(false, true);
		}

		private static string DbFile = "firstProject.db";
	}
}
